package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();//通信通道

        //1.定义随机队列 2.定义交换机 3.绑定
        String queue = UUID.randomUUID().toString();
        //非持久化 独占 自动删除
        c.queueDeclare(queue,false, true, true, null);
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);//生产者和消费者都定义了交换机,则先启动哪一个都可以绑定
        //第三个参数对funout无效
        c.queueBind(queue, "logs", "");//做绑定
        //从随机队列正常地消费数据
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody());
                System.out.println("收到:"+msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        c.basicConsume(queue, true, deliverCallback,cancelCallback);
    }
}
